<?php

namespace app\api\model;

use app\admin\model\MemberModel;
use think\Model;


class CompanyOrderModel extends ApiModel
{

    

    

    // 表名
    protected $name = 'company_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;
    protected $deleteTime = false;


    protected $status = [0 => '未支付', 1 => '已支付', 2 => "取消订单"];





    public function member()
    {
        return $this->belongsTo(MemberModel::class, 'member_id', 'id', [], 'LEFT');
    }


    public function getPackageOrder($params){
        $status = $params['status'] ?? 1;
        if($params['status'] != ''){
            $map['status'] = $status;
        }
        $map['member_id'] =$params['uid'];
        $order = self::where($map)
            ->field('id,out_trade_no,company_cate_name,
            company_content,company_package_name,spec_name,status,
            pay_amount,status,unit,pay_time,realname,phone,notes,
            company_image,company_type_name,pay_type')
            ->order('create_time desc')
            ->paginate(10);
        foreach ($order as &$value){
            $value['status_name'] = $this->status[$value['status']] ?? '';
            $value['company_content'] = json_decode($value['company_content'], true);
            $value['company_content_desc'] = count($value['company_content'])."项服务明细";
//            $value['pay_time'] = date('Y-m-d H:i:s', $value['pay_time']);
            $value['pay_time'] = $value['pay_time'] ? date('Y-m-d H:i:s', $value['pay_time']) : '';

            $value['company_image'] = config('app.image_url').$value['company_image'];
        }

        return $this->api_success('获取成功',$order);
    }

    public function cancelPackageOrder($params){
        $order_id = $params['order_id'] ?? '';
        $order = self::where(['out_trade_no' => $order_id,'status'=>0])->find();
        if(!$order){
           return $this->api_error('订单不存在');
        }
        if(!self::where(['out_trade_no' => $order_id])->update(['status' => 2])){
          return  $this->api_error('操作失败，请刷新后重试');
        }
        return $this->api_success('取消成功');

    }


    public function getPackageOrderDetail($params = array()){
        $order_id = $params['order_id'] ?? '';
        $order = self::where(['out_trade_no' => $order_id])
            ->field('id,out_trade_no,company_cate_name,
            company_content,company_package_name,spec_name,status,
            pay_amount,status,unit,create_time,pay_time,realname,phone,notes,
            company_image,company_type_name,expire_time,pay_type')
            ->find();
        if (!$order) {
            return $this->api_error('订单不存在');
        }

        $order['status_name'] = $this->status[$order['status']] ?? '';

        $content = json_decode($order['company_content'], true);
        $order['company_content'] = array_map(function($key, $value) {
            return array('key' => $key, 'value' => $value);
        }, array_keys($content), $content);
        $order['company_content_desc'] = count($order['company_content'])."项服务明细";
        $order['pay_time'] = $order['pay_time'] ? date('Y-m-d H:i:s', $order['pay_time']) : '';
        $order['expire_time'] = $order['expire_time'] ? date('Y-m-d H:i:s', $order['expire_time']) : '';
        $order['company_image'] = config('app.image_url').$order['company_image'];


        return $this->api_success('获取成功', $order);
    }

}
